package com.fpost.form.dto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.fpost.domain.Article;
import com.fpost.domain.ArticleComment;
import com.fpost.domain.ArticleRatingSummary;
import com.fpost.domain.ArticleVersion;
import com.fpost.helper.Byline;
import com.fpost.manager.ArticleCommentManager;
import com.fpost.manager.ArticleManager;
import com.fpost.manager.ArticleTypeManager;
import com.fpost.util.CommonUtils;
import com.fpost.util.DateUtil;
import com.fpost.util.Formatter;

public class ArticleDTO {

	private String articleId;
	private String articleTypeId;
	private String articleType;
	private String authorId;
	private String authorUsername;
	private String authorFirstName;
	private String authorLastName;
	private String title;
	private String summary;
	private String body;
	private String createdOn;	
	private String createdIp;
	private String keywords;
	private String hitCount;
	private String deleted;
	private String averageRating;
	private String numberOfRatings;
	
	private Byline byline;
	private List<ArticleVersionDTO> articleVersionDTOList;
	private List<ArticleCommentDTO> articleCommentDTOList;
	
	public ArticleDTO() {
		super();
	}

	public ArticleDTO(Article article, boolean search) throws Exception {
		
		this.articleId = String.valueOf(article.getArticleId());
		this.authorId = String.valueOf(article.getUser().getUserId());
		this.authorUsername = article.getUser().getUsername();
		this.authorFirstName = article.getUser().getFirstName();
		this.authorLastName = article.getUser().getLastName();
		
		ArticleVersion latestVersion = article.getVersions().get(0);
		
		this.title = latestVersion.getTitle();
		this.summary = CommonUtils.replaceNewline(latestVersion.getSummary());
		this.createdOn = DateUtil.dateToString(latestVersion.getCreatedOn(),
				DateUtil.COMPLEX_DATE_FORMAT_TIME_ZONE);
		
		this.createdIp = CommonUtils.privateIpAddress(latestVersion.getCreatedIp());
		this.hitCount = String.valueOf(article.getHitCount());
		this.deleted = String.valueOf(article.isDeleted());
		
		// calculate and set rating
		ArticleRatingSummary articleRatingSummary = article.calculateArticleRating();
		this.averageRating = String.valueOf(Formatter.parseDecimal(articleRatingSummary.getAverageRating(), "#.#"));
		this.numberOfRatings = String.valueOf(articleRatingSummary.getNumberOfRatings());		
		
		if (!search) {
			this.articleType = new ArticleTypeManager().get(latestVersion.getArticleTypeId()).getArticleType();
			this.body = CommonUtils.replaceNewline(latestVersion.getBody());
			this.keywords = latestVersion.getKeywords();
			this.articleVersionDTOList = setArticleVersionDTOList(article);
			this.articleCommentDTOList = setArticleCommentDTOList(article.getArticleId());
		}
	}	

	public List<ArticleVersionDTO> getArticleVersionDTOList() {
		return articleVersionDTOList;
	}

	private List<ArticleVersionDTO> setArticleVersionDTOList(Article article) {
		List<ArticleVersionDTO> articleVersionDTOList = new ArrayList<ArticleVersionDTO>();
		for (ArticleVersion articleVersion : article.getVersions()) {			
			ArticleVersionDTO articleVersionDTO = new ArticleVersionDTO();
			articleVersionDTO.setPropertiesForVersionLink(articleVersion);
			articleVersionDTOList.add(articleVersionDTO);
		}
		return articleVersionDTOList;
	}
	
	public int getArticleVersionDTOListSize() {
		return articleVersionDTOList==null?0:articleVersionDTOList.size();
	}
	
	public List<ArticleCommentDTO> getArticleCommentDTOList() {
		return articleCommentDTOList;
	}

	private List<ArticleCommentDTO> setArticleCommentDTOList(long articleId) {
		List<ArticleComment> comments = 
				new ArticleCommentManager().getCommentsByArticleId(articleId);
		List<ArticleCommentDTO> articleCommentDTOList = new ArrayList<ArticleCommentDTO>();
		for (ArticleComment articleComment : comments) {			
			articleCommentDTOList.add(new ArticleCommentDTO(articleComment));
		}		
		return articleCommentDTOList;
	}

	public void setSnapshotProperties(Article article) throws Exception {
		this.articleId = String.valueOf(article.getArticleId());
		ArticleVersion latestVersion = article.getVersions().get(0);
		this.title = latestVersion.getTitle();
		this.hitCount = String.valueOf(article.getHitCount());
	}
	
	public void setPropertiesForHighestRating(Object[] objectArr) throws Exception {
		
		long articleId = ((BigDecimal)objectArr[0]).longValue();
		this.articleId = String.valueOf(articleId);
		
		Article article = new ArticleManager().get(articleId);
		this.title = article.getVersions().get(0).getTitle();
		
		double averageRating = ((BigDecimal)objectArr[1]).doubleValue();
		this.averageRating = String.valueOf(Formatter.parseDecimal(averageRating, "#.#"));
		
		long numberOfRatings = ((BigDecimal)objectArr[2]).longValue();
		this.numberOfRatings = String.valueOf(numberOfRatings);
	}
	
	public String getArticleId() {
		return articleId;
	}

	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}

	public String getArticleType() {
		return articleType;
	}

	public void setArticleType(String articleType) {
		this.articleType = articleType;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(String createdOn) {
		this.createdOn = createdOn;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getAuthorUsername() {
		return authorUsername;
	}

	public void setAuthorUsername(String authorUsername) {
		this.authorUsername = authorUsername;
	}

	public String getAuthorFirstName() {
		return authorFirstName;
	}

	public void setAuthorFirstName(String authorFirstName) {
		this.authorFirstName = authorFirstName;
	}

	public String getAuthorLastName() {
		return authorLastName;
	}

	public void setAuthorLastName(String authorLastName) {
		this.authorLastName = authorLastName;
	}

	public String getArticleTypeId() {
		return articleTypeId;
	}

	public void setArticleTypeId(String articleTypeId) {
		this.articleTypeId = articleTypeId;
	}

	public String getCreatedIp() {
		return createdIp;
	}

	public void setCreatedIp(String createdIp) {
		this.createdIp = createdIp;
	}

	public String getHitCount() {
		return hitCount;
	}

	public void setHitCount(String hitCount) {
		this.hitCount = hitCount;
	}

	public String getAuthorId() {
		return authorId;
	}

	public void setAuthorId(String authorId) {
		this.authorId = authorId;
	}

	public String getDeleted() {
		return deleted;
	}

	public void setDeleted(String deleted) {
		this.deleted = deleted;
	}

	public String getAverageRating() {
		return averageRating;
	}

	public void setAverageRating(String averageRating) {
		this.averageRating = averageRating;
	}

	public String getNumberOfRatings() {
		return numberOfRatings;
	}

	public void setNumberOfRatings(String numberOfRatings) {
		this.numberOfRatings = numberOfRatings;
	}
	
	public Byline getByline() {
		return new Byline(this);
	}
	
	//	public Article convertToDomainObj() {
	//	Article article = new Article();
	//	article.setArticleId(Formatter.parseLong(this.articleId));
	//	article.setUser(new UserManager().get(Long.parseLong(this.userId)));
	//	article.setTitle(this.title);
	//	article.setBody(this.body);
	//	article.setCreatedOn(DateUtil.stringToDate(this.createdOn,
	//			DateUtil.SIMPLE_DATE_FORMAT));
	//	article.setModifiedOn(DateUtil.stringToDate(this.modifiedOn,
	//			DateUtil.SIMPLE_DATE_FORMAT));
	//	article.setKeywords(this.keywords);
	//	return article;
	//}
}
